package org.sky.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.sky.lock.RedisLockUtil;
import org.sky.lock.annotation.Lock;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class LockAspceJAdvice {

	@Around("execution(* org.sky.service..*(..))")
	public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("aop start");
		Signature signature = pjp.getSignature();  
		MethodSignature methodSignature = (MethodSignature) signature;  
		Lock lock = methodSignature.getMethod().getAnnotation(Lock.class);
		if(lock!=null){
			String key = lock.key();
			Object retVal ;
			if(RedisLockUtil.lock(key,1L)){
				retVal = pjp.proceed();
				RedisLockUtil.unLock(key);
			}else{
				retVal = pjp.proceed();
			}
			return retVal;
		}else{
			Object retVal = pjp.proceed();
			return retVal;
		}
	}
}
